package com.whg.demo.controller;

import com.whg.demo.biz.RedisClient;
import com.whg.demo.model.JsonData;
import com.whg.demo.model.SetSetDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Set;

/**
 * @Author whg
 * @create 2022/12/8 11:26
 * @description：
 */
@RestController
@RequestMapping("redis")
public class RedisController {

    @Autowired
    private RedisClient redisClient;

    @GetMapping("/set")
    public JsonData set(@RequestParam("key") String key,@RequestParam("value") String value){
        redisClient.set(key,value);
        return JsonData.buildSuccess();
    }

    @GetMapping("/get")
    public JsonData get(@RequestParam("key") String key){
        String value = redisClient.get(key);
        return JsonData.buildSuccess(value);
    }

    @GetMapping("/setList")
    public JsonData setList(@RequestParam("key") String key,@RequestParam("value") String value){
        List<String> list = Arrays.asList(value.split(","));
        redisClient.setList(key,list);
        return JsonData.buildSuccess();
    }

    @GetMapping("/getList")
    public JsonData getList(@RequestParam("key") String key){
        List<String> list = redisClient.getList(key);
        return JsonData.buildSuccess(list);
    }

    @GetMapping("/append")
    public JsonData append(@RequestParam("key") String key,@RequestParam("value") String value){
        redisClient.append(key,value);
        return JsonData.buildSuccess();
    }

    @GetMapping("/delete")
    public JsonData delete(@RequestParam("key") String key,@RequestParam("value") String value){
        redisClient.delete(key,value);
        return JsonData.buildSuccess();
    }


    @PostMapping("/setSet")
    public JsonData setSet(@RequestBody SetSetDTO dto){
        redisClient.setSet(dto.getKey(),dto.getValue());
        return JsonData.buildSuccess();
    }

    @GetMapping("/getSet")
    public JsonData getSet(@RequestParam("key") String key){
        Set<String> set = redisClient.getSet(key);
        return JsonData.buildSuccess(set);
    }

    @GetMapping("/appendSet")
    public JsonData appendSet(@RequestParam("key") String key,@RequestParam("value") String value){
        redisClient.appendSet(key,value);
        return JsonData.buildSuccess();
    }

    @GetMapping("/deleteSet")
    public JsonData deleteSet(@RequestParam("key") String key,@RequestParam("value") String value){
        redisClient.deleteSet(key,value);
        return JsonData.buildSuccess();
    }


}
